package product;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class Sale extends JInternalFrame {

	DefaultTableModel model;// JTable에 입력을위해
	Vector<String> col = new Vector<String>();
	DecimalFormat df = new DecimalFormat("###,###,###.##");
	ArrayList<SaleData> aSal;
	HashMap<String, SaleTotal> hSal;
	HashMap<String, StockData> sto;
	static int saleCnt;

	private JPanel panel;
	private JTextField findStr;
	private JButton btnNewButton;
	private JScrollPane result;
	private JTable table;
	private JComboBox comboBox;

	/**
	 * Create the frame.
	 */
	public Sale(HashMap<String, StockData> s, ArrayList<SaleData> a,
			HashMap<String, SaleTotal> h, int cnt) {
		super("Sale", true, true, true, false);
		this.sto = s;
		this.aSal = a;
		this.hSal = h;
		this.saleCnt = cnt;
		col.add("제품코드");
		col.add("제품명");
		col.add("단가");
		col.add("재고수량");
		model = new DefaultTableModel(col, 0) {
			public boolean isCellEditable(int rowIndex, int mColIndex) {
				return false;
			}
		};
		setBounds(100, 100, 442, 290);
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getResult(), BorderLayout.CENTER);

		// 테이블 형태지정
		DefaultTableCellRenderer Center = new DefaultTableCellRenderer();
		Center.setHorizontalAlignment(SwingConstants.CENTER);// 가운데 정렬
		DefaultTableCellRenderer Right = new DefaultTableCellRenderer();
		Right.setHorizontalAlignment(SwingConstants.RIGHT);// 우측 정렬
		TableColumnModel tcmSchedule = table.getColumnModel();
		tcmSchedule.getColumn(0).setCellRenderer(Center);
		tcmSchedule.getColumn(1).setCellRenderer(Center);
		tcmSchedule.getColumn(2).setCellRenderer(Right);// 단가
		tcmSchedule.getColumn(3).setCellRenderer(Center);

		table.getTableHeader().setReorderingAllowed(false);
		table.getTableHeader().setResizingAllowed(false);
		table.getColumn("제품코드").setPreferredWidth(50);
		table.getColumn("제품명").setPreferredWidth(50);
		table.getColumn("단가").setPreferredWidth(70);
		table.getColumn("재고수량").setPreferredWidth(30);
		table.setRowHeight(20);// 테이블 상하폭지정

	}

	public String replace(String s) {
		String temp = s.replace(",", "");
		String temp2 = temp.replace("원", "");
		return temp2;
	}

	public static void setSaleCnt(int cnt) {
		// TODO Auto-generated method stub
		saleCnt = cnt;

	}

	public void search() {

		if (model.getRowCount() > 0) { // 행의 수
			for (int i = model.getRowCount() - 1; i > -1; i--) {
				model.removeRow(i);// 테이블을 초기화
			}
		}
		String find = findStr.getText();// ArrayList에 저장되어있는 정보들을 JTable에
										// 추가

		if (comboBox.getSelectedIndex() == 0) {
			Set<String> st = sto.keySet();
			Iterator<String> it = st.iterator();
			// index번호 순서대로
			while (it.hasNext()) {
				Vector<String> row = new Vector<String>();
				String key = it.next();
				StockData value = sto.get(key);
				if (value.getProName().contains(find)) {
					row.add(value.getProCode());
					row.add(value.getProName());
					row.add(replace(String.valueOf(value.getProPrice())));
					row.add(replace(String.valueOf(value.getProEa())));

					model.addRow(row);
				}
			}
		} else if (comboBox.getSelectedIndex() == 1) {
			Set<String> st = sto.keySet();
			Iterator<String> it = st.iterator();
			// index번호 순서대로
			while (it.hasNext()) {
				Vector<String> row = new Vector<String>();
				String key = it.next();
				StockData value = sto.get(key);
				if (value.getProCode().contains(find)) {
					row.add(value.getProCode());
					row.add(value.getProName());
					row.add(replace(String.valueOf(value.getProPrice())));
					row.add(replace(String.valueOf(value.getProEa())));

					model.addRow(row);
				}
			}
		}
	}

	public void mouseC() {
		int index = table.getSelectedRow();// 선택된 열의 table index번호를 저장
		Vector<String> vec = new Vector<String>();// 벡터생성
		vec.add((String) table.getValueAt(index, 0));// 코드
		vec.add((String) table.getValueAt(index, 1));// 이름
		vec.add(replace((String) table.getValueAt(index, 2)));// 단가
		vec.add((String) table.getValueAt(index, 3));// 재고
		// 자리별 정보를 Vector에 추가

		SaleInput si = new SaleInput(sto, vec, model, aSal, hSal, saleCnt,
				index);
		if (si != null) {
			// sit
		}

		si.setVisible(true);
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getFindStr(), BorderLayout.CENTER);
			panel.add(getBtnNewButton(), BorderLayout.EAST);
			panel.add(getComboBox(), BorderLayout.WEST);
		}
		return panel;
	}

	private JTextField getFindStr() {
		if (findStr == null) {
			findStr = new JTextField();
			findStr.setColumns(10);
		}
		return findStr;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("조회");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					search();// 조회 매서드
				}
			});
		}
		return btnNewButton;
	}

	private JScrollPane getResult() {
		if (result == null) {
			result = new JScrollPane();
			result.setViewportView(getTable());
		}
		return result;
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					mouseC();// 마우스 클릭 매서드
				}
			});
		}
		return table;
	}

	private JComboBox getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox();
			comboBox.setModel(new DefaultComboBoxModel(new String[] { "제품명",
					"제품코드" }));
		}
		return comboBox;
	}
}
